﻿Ext.util.Dispatcher = Ext.extend(Ext.util.Observable, {

    constructor: function (config) {
        this.addEvents(

            'before-dispatch',


            'dispatch'
        );

        Ext.util.Dispatcher.superclass.constructor.call(this, config);
    },


    dispatch: function (options) {
        var interaction = new Ext.Interaction(options),
            controller = interaction.controller,
            action = interaction.action,
            History = Ext.History;

        if (this.fireEvent('before-dispatch', interaction) !== false) {
            if (History && options.historyUrl) {
                History.suspendEvents(false);
                History.add(options.historyUrl);
                Ext.defer(History.resumeEvents, 100, History);
            }

            if (controller && action) {
                controller[action].call(controller, interaction);
                interaction.dispatched = true;
            }

            this.fireEvent('dispatch', interaction);
        }
    },


    redirect: function (options) {
        if (options instanceof Ext.data.Model) {


        } else if (typeof options == 'string') {

            var route = Ext.Router.recognize(options);

            if (route) {
                return this.dispatch(route);
            }
        }
        return null;
    },


    createRedirect: function (url) {
        return function () {
            Ext.Dispatcher.redirect(url);
        };
    }
});


Ext.Dispatcher = new Ext.util.Dispatcher();


Ext.dispatch = function () {
    return Ext.Dispatcher.dispatch.apply(Ext.Dispatcher, arguments);
};


Ext.redirect = function () {
    return Ext.Dispatcher.redirect.apply(Ext.Dispatcher, arguments);
};

Ext.createRedirect = Ext.Dispatcher.createRedirect;